﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinQ_DTO;

namespace DAO
{
    public class UserDAO
    {
        public List<aspnet_User> LayDanhSach()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var userList = from c in EFashionDB.aspnet_Users select c;
            return userList.ToList();
        }

        public aspnet_User LayUserTheoId(Guid ma)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var user = from c in EFashionDB.aspnet_Users
                       where c.UserId == ma
                       select c;
            return user.Single();

        }

        public void Them(aspnet_User user)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.aspnet_Users.InsertOnSubmit(user);
            EFashionDB.SubmitChanges();

        }

        public void Xoa(Guid ma)
        {

            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();

            aspnet_User newuser = EFashionDB.aspnet_Users.Single(u => u.UserId == ma);

            EFashionDB.aspnet_Users.DeleteOnSubmit(newuser);
            EFashionDB.SubmitChanges();

        }

        public void CapNhat(aspnet_User user)
        {

            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();

            aspnet_User newuser = EFashionDB.aspnet_Users.Single(u => u.UserId == user.UserId);

            newuser.UserId = user.UserId;
            newuser.UserName = user.UserName;
            newuser.ApplicationId = user.ApplicationId;
            newuser.IsAnonymous = user.IsAnonymous;
            newuser.LastActivityDate = user.LastActivityDate;
            newuser.LoweredUserName = user.LoweredUserName;
            newuser.MobileAlias = user.MobileAlias;


            EFashionDB.SubmitChanges();

        }



////////////////////////////// CODE BY NMCUONG/////////////////////////////////


        public aspnet_User LayUserTheoTen(string UserName)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();

            //aspnet_User product = EFashionDB.aspnet_Users.Single(p => p.UserName == UserName);
            var user = from ur in EFashionDB.aspnet_Users
                       where ur.UserName == UserName
                       select ur;
            return user.Single();
        }

        public aspnet_User LayUserTheoMa(Guid UserId)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();

            //aspnet_User product = EFashionDB.aspnet_Users.Single(p => p.UserName == UserName);
            var user = from ur in EFashionDB.aspnet_Users
                       where ur.UserId == UserId
                       select ur;
            return user.Single();
        }

        /************************************************************************************************************        *                          coder: TRAN KINH LY 
         ***********************************************************************************************************/
        public List<aspnet_User> TimUserTheo_Khoa_LoweredRoleName(String key_search, String lrn)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();

            var list = from user in EFashionDB.aspnet_Users
                       from usersinrole in EFashionDB.aspnet_UsersInRoles
                       from role in EFashionDB.aspnet_Roles
                       where user.UserName.Contains(key_search) &&
                            user.UserId == usersinrole.UserId &&
                            usersinrole.RoleId == role.RoleId &&
                            role.LoweredRoleName == lrn
                       select user;
            return list.ToList();
        }

        /************************************************************************************************************        *                          coder: TRAN KINH LY 
         ***********************************************************************************************************/


    }
}
